//头盔
import * as THREE from "../node_modules/three/build/three.module.js";
import { GLTFLoader } from "../node_modules/three/examples/jsm/loaders/GLTFLoader";
import { OrbitControls } from "../node_modules/three/examples/jsm/controls/OrbitControls.js";

var scene, camera, light, renderer, loader, controls;

function init() {
  scene = new THREE.Scene();
  //   scene.background = new THREE.Color(0x2c3e50);
  let bgTexture = new THREE.TextureLoader().load("../src/img/rick&motty.png");
  bgTexture.wrapS = THREE.RepeatWrapping;
  bgTexture.wrapT = THREE.RepeatWrapping;
  bgTexture.repeat.set(1, 1);
  scene.background = bgTexture;

  camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    100
  );
  camera.position.set(0, 0, 3);
  camera.lookAt(new THREE.Vector3(0, 0, 0));
  scene.add(camera);

  renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  controls = new OrbitControls(camera, renderer.domElement);
  controls.update();

  //加载模型
  loader = new GLTFLoader();

  loader.load(
    "../src/models/glTF/DamagedHelmet.gltf",
    function (gltf) {
      scene.add(gltf.scene);
    },
    undefined,
    function (error) {
      console.log(error);
    }
  );

  //模型需要光源照亮,不然是黑乎乎一片
  var light = new THREE.AmbientLight(0xe74c3c, 2); // soft white light
  scene.add(light);
}

function animate() {
  requestAnimationFrame(animate);
  controls.update();
  renderer.render(scene, camera);
}

init();
animate();
